package com.msunsoft.dataset.dictionary.validator;

import java.util.List;

import org.pentaho.di.core.CheckResult;
import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.metastore.api.IMetaStore;
import org.w3c.dom.Node;

public class ValidatorMeta extends BaseStepMeta implements StepMetaInterface {
    private static Class<?> PKG = ValidatorMeta.class; // for i18n purposes,
                                                       // needed by
                                                       // Translator2!!

    private DatabaseMeta databaseMeta;
    
    private String tableName;//进行校验的表名称

    public ValidatorMeta() {
        super(); // allocate BaseStepMeta
    }

    // TODO XUCL 2015年3月21日20:33:06 从ktr文件中加载数据配置信息与SQL语句
    public void loadXML(Node stepnode, List<DatabaseMeta> databases, IMetaStore metaStore) throws KettleXMLException {
        databaseMeta = DatabaseMeta.findDatabase(databases, XMLHandler.getTagValue(stepnode, "connection"));
        tableName = XMLHandler.getTagValue(stepnode, "tableName");
    }

    // TODO XUCL 2015年3月21日20:34:41 将转换转成XML
    public String getXML() {
        StringBuffer retval = new StringBuffer(300);
        retval.append("    " + XMLHandler.addTagValue("connection", databaseMeta == null ? "" : databaseMeta.getName()));
        retval.append("    " + XMLHandler.addTagValue("tableName", tableName));
        return retval.toString();
    }

    public boolean equals(Object obj) {
        if (obj != null && (obj.getClass().equals(this.getClass()))) {
            ValidatorMeta m = (ValidatorMeta) obj;
            return (getXML() == m.getXML());
        }

        return false;
    }

    public Object clone() {
        ValidatorMeta retval = (ValidatorMeta) super.clone();
        return retval;
    }

    public void setDefault() {
    }

    public void readRep(Repository rep, IMetaStore metaStore, ObjectId id_step, List<DatabaseMeta> databases) throws KettleException {
        databaseMeta = rep.loadDatabaseMetaFromStepAttribute(id_step, "id_connection", databases);
        tableName = rep.getStepAttributeString(id_step, "tableName");
    }

    // TODO XUCL 2015年3月21日20:36:44 保存转换到资源库中
    public void saveRep(Repository rep, IMetaStore metaStore, ObjectId id_transformation, ObjectId id_step) throws KettleException {
        rep.saveDatabaseMetaStepAttribute(id_transformation, id_step, "id_connection", databaseMeta);
        rep.saveStepAttribute(id_transformation, id_step, "tableName", tableName);
    }

    public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, VariableSpace space,
            Repository repository, IMetaStore metaStore) {
        CheckResult cr;
        if (prev == null || prev.size() == 0) {
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_WARNING, BaseMessages.getString(PKG, "ValidatorMeta.CheckResult.ExpectedInputError"), stepMeta);
            remarks.add(cr);
        } else {
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ValidatorMeta.CheckResult.FieldsReceived", "" + prev.size()), stepMeta);
            remarks.add(cr);
        }

        // See if we have input streams leading to this step!
        if (input.length > 0) {
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ValidatorMeta.CheckResult.ExpectedInputOk"), stepMeta);
            remarks.add(cr);
        } else {
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "ValidatorMeta.CheckResult.ExpectedInputError"), stepMeta);
            remarks.add(cr);
        }
    }

    public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int cnr, TransMeta tr, Trans trans) {
        return new Validator(stepMeta, stepDataInterface, cnr, tr, trans);
    }

    public StepDataInterface getStepData() {
        return new ValidatorData();
    }

    public boolean supportsErrorHandling() {
        return true;
    }

    public boolean excludeFromRowLayoutVerification() {
        return true;
    }

    public DatabaseMeta getDatabaseMeta() {
        return this.databaseMeta;
    }
    public void setDatabaseMeta(DatabaseMeta databaseMeta) {
        this.databaseMeta = databaseMeta;
    }

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

}
